define(['jq', 'common'], () => {
    //首部复用
    $('header').load('../html/header.html')
    //尾部复用
    $('footer').load('../html/footer.html')
    setTimeout(() => {
        class Collection {
            constructor() {
                this.del = document.querySelector('.del')
                this.ul = document.querySelector('.collect_ul')
                this.del = document.querySelector('.del')
                this.collect = document.querySelector('.collect')
                this.collectlist = JSON.parse(localStorage.getItem('collection')) || []
                this.init()
            }
            init() {
                this.render()
                this.addEvent()
                this.count()
            }
            render() {
                this.ul.innerHTML = ''
                // console.log(this.collectlist);
                if (this.collectlist.length > 0) {
                    this.collectlist.forEach((item) => {
                        this.ul.innerHTML += `
                        <li>
                        <img src="${item.img_small_logo}">
                        <span class="title">${item.title}</span>
                        <span class="price">¥${item.price}</span>
                        <span class="del" data-id="${item.goods_id}">删除</span>
                        </li>
                    `
                    })
                } else {
                    this.ul.innerHTML += `
                    <li>
                    <img src="../img/gouwuche.png">
                    <span class="title">你的收藏夹里什么也没有哦~</span>
                    <span class="price"><a href="../html/list.html">去逛逛</a></span>
                    </li>
                `
                }
            }
            addEvent() {
                this.collect.onclick = (e) => {
                    let target = e.target || e.srcElement
                    //删除商品功能
                    if (target.className == 'del') {
                        let id = target.dataset.id - 0
                        // 获取所对应的id所对应的商品
                        let index = this.collectlist.findIndex((item) => {
                            return item.goods_id == id
                        })
                        console.log(index);
                        // splice()方法
                        this.collectlist.splice(index, 1)
                        localStorage.setItem('collection',JSON.stringify(this.collectlist))
                        // 重新渲染数据
                        this.render()
                    }
                }
            }
            count() {
                let em = document.querySelector('em')
                let list = JSON.parse(localStorage.getItem('cart')) || []
                let count = 0
                list.map((item) => {
                    count += item.cart_number
                    em.innerHTML = count
                })
            }
        }
        new Collection()

    }, 500)
})